package com.yazi.mxz.common.db;



import java.util.List;

import org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.ReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.TableIdentifier;
import org.hibernate.mapping.ForeignKey;

public class NamingStrategy extends DelegatingReverseEngineeringStrategy {

	public NamingStrategy(ReverseEngineeringStrategy delegate) {
		super(delegate);
		
	}

	@Override
	public String tableToClassName(TableIdentifier tableIdentifier) {
		
		String className = super.tableToClassName(tableIdentifier);
		String classSimpleName = className.substring(className.lastIndexOf(".") + 1);
		if (classSimpleName.indexOf("Mxz") > -1) {
			classSimpleName = classSimpleName.substring(3);
			className = className.substring(0, className.lastIndexOf(".") + 1) + classSimpleName;
		}
		return className;
	}

	
	@Override
	public String foreignKeyToCollectionName(String keyname, TableIdentifier fromTable, List fromColumns, TableIdentifier referencedTable, List referencedColumns, boolean uniqueReference) {
		
		String foreignKeyToCollectionName = super.foreignKeyToCollectionName(keyname, fromTable, fromColumns, referencedTable, referencedColumns, uniqueReference);
		if (foreignKeyToCollectionName.startsWith("mxz")) {
			foreignKeyToCollectionName = foreignKeyToCollectionName.substring(3);
			foreignKeyToCollectionName = foreignKeyToCollectionName.substring(0, 1).toLowerCase() + foreignKeyToCollectionName.substring(1);
		}
		return foreignKeyToCollectionName; 
	}

	@Override
	public String foreignKeyToEntityName(String keyname, TableIdentifier fromTable, List fromColumnNames, TableIdentifier referencedTable, List referencedColumnNames, boolean uniqueReference) {
		
		String foreignKeyToEntityName = super.foreignKeyToEntityName(keyname, fromTable, fromColumnNames, referencedTable, referencedColumnNames, uniqueReference);
		if (foreignKeyToEntityName.startsWith("mxz")) {
			foreignKeyToEntityName = foreignKeyToEntityName.substring(3);
			foreignKeyToEntityName = foreignKeyToEntityName.substring(0, 1).toLowerCase() + foreignKeyToEntityName.substring(1);
		}
		return foreignKeyToEntityName;
	}

	@Override
	public String foreignKeyToManyToManyName(ForeignKey fromKey, TableIdentifier middleTable, ForeignKey toKey, boolean uniqueReference) {
		
		String foreignKeyToManyToManyName = super.foreignKeyToManyToManyName(fromKey, middleTable, toKey, uniqueReference);
		if (foreignKeyToManyToManyName.startsWith("mxz")) {
			foreignKeyToManyToManyName = foreignKeyToManyToManyName.substring(3);
			foreignKeyToManyToManyName = foreignKeyToManyToManyName.substring(0, 1).toLowerCase() + foreignKeyToManyToManyName.substring(1);
		}
		return foreignKeyToManyToManyName;	 
	}

}
